﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.DataAccess.Business {
    public class PriceEvaluateRepository : BaseRepository<PriceEvaluate> {

        private int _pageSize;

        public PriceEvaluateRepository(int pageSize) {
            this._pageSize = pageSize;
        }

        public PriceEvaluate FetchPriceEvaluateById(int priceEvaluateId) {
            using (var ctx = this.Context) {
                return ctx.PriceEvaluates.Find(priceEvaluateId);
            }
        }

        public List<PriceEvaluate> FetchPriceEvaluateList() {
            using (var ctx = this.Context) {
                return ctx.PriceEvaluates
                    .Include("Basic")
                    .Include("Basic1")
                    .Include("Product")
                    .Include("Customer")
                    .ToList();
            }
        }

        public List<PriceEvaluate> FetchPriceEvaluateList(PriceEvaluateSearchModel searchModel, int pageIndex) {
            using (var ctx = this.Context) {
                var query = ctx.PriceEvaluates
                    .Include("Basic")
                    .Include("Basic1")
                    .Include("Product")
                    .Include("Customer")
                    .Where(x => x.PriceEvaluateId != 0);
                
                if (searchModel.CustomerId.HasValue) {
                    query = query.Where(x => x.CustomerId == searchModel.CustomerId.Value);
                }
                if (searchModel.ProductId.HasValue) {
                    query = query.Where(x => x.ProductId == searchModel.ProductId.Value);
                }
                if (searchModel.PayCurrencyId.HasValue) {
                    query = query.Where(x => x.PayCurrencyId == searchModel.PayCurrencyId.Value);
                }
                if (searchModel.PaymentTypeId.HasValue) {
                    query = query.Where(x => x.PaymentTypeId == searchModel.PaymentTypeId.Value);
                }                

                searchModel.Records = query.Count();
                pageIndex = (pageIndex - 1) * this._pageSize > searchModel.Records ? 1 : pageIndex;

                return query.OrderByDescending(x => x.CreatedDatetime).Skip((pageIndex - 1) * this._pageSize).Take(this._pageSize).ToList();
            }
        }

        public void DeleteMultiplePriceEvaluate(params int[] priceEvaluateIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.PriceEvaluates.Where(x => priceEvaluateIds.Contains(x.PriceEvaluateId)).ToList().ForEach(x => {
                            ctx.PriceEvaluates.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }
    }
}
